package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ManySheetOneSyler;
import com.jy.wms.pojo.WmsproductWarning;
import com.jy.wms.service.WmsproductWarningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/wmsproductWarning")
@Api(value = "/wmsproductWarning", description = "产品预警分析")
public class WmsproductWarningController extends BaseController<WmsproductWarningService,WmsproductWarning> {
    @Resource
    private WmsproductWarningService wmsproductWarningService;

    @PostMapping(value = "/baseHarvestPage")
    @ApiOperation(value = "产品预警分析报表",response = JSONObject.class, notes = "产品预警分析报表分页查询")
    public @ResponseBody
    JSONObject  baseHarvestPage(@RequestBody WmsproductWarning entity, HttpSession session) {
        Page<WmsproductWarning> list =new Page<WmsproductWarning>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            list= baseService.baseHarvestPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 导出产品预警信息
     *
     * @param response
     * @param
     * @param session
     * @return
     * @throws IOException
     */
    @PostMapping(value = "exportWmsproductWarning")
    @ApiOperation(value = "导出产品预警信息", response = JSONObject.class, notes = "导出产品预警信息")
    public @ResponseBody void exportWmsproductWarning(HttpServletResponse response,@RequestBody  WmsproductWarning entity, HttpSession session) throws IOException {
        String fileName = "产品预警信息";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        //WmsproductWarning entity=new WmsproductWarning();
        entity.setCkid(ckid);
        List<WmsproductWarning> WmsproductWarningList = baseService.exportWmsproductWarning(entity);
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 文件名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("产品预警信息", "UTF-8") + ".xls");
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsproductWarning.class, WmsproductWarningList);
        workbook.write(response.getOutputStream());
    }
}
